(mqtt学习6)python实现遗嘱,拔网线之后,可以发出遗嘱消息 |
您所在的位置:网站首页 › python paho mqtt client › (mqtt学习6)python实现遗嘱,拔网线之后,可以发出遗嘱消息 |
import paho.mqtt.client as mqtt
HOST = "192.168.1.202"
PORT = 1883
def on_connect(client, userdata, flags, rc):
print("successfully connected with result code "+str(rc))
def Test():
client = mqtt.Client()
client.on_connect = on_connect
client.will_set("test/smartfire/will", "last will message", 0, False)
client.connect(HOST, PORT, 1)
client.publish("test/smartfire/first", "hello world!", 2)
client.loop_forever()
if __name__ == '__main__':
Test()
说明: client.will_set("test/smartfire/will", "last will message", 0, False) 第一个参数,话题名称; 第二个参数,话题内容; 第三个参数,qos,消息质量; 第四个参数,retained,是否保留到服务器。 client.connect(HOST, PORT, 1) 第三个参数,设置心跳时间,即拔掉网线1s就服务器会发出遗嘱。 client.publish("test/smartfire/first", "hello world!", 2) 注意这3个函数的使用,设置遗嘱,连接,然后再发布。 在服务器端监听: 因为里面这个ip被我映射过了,所以我打了码。 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |